﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Product
{
    public partial class View : BasePage_BackEnd
    {
        //todo wangdi
        private const string ProductId = "ProductId";
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!IsPostBack)
            {
                PagerControll_PageChange(pager.CurrentPageIndex);
            }
        }

        protected void PagerControll_PageChange(int pageIndex)
        {
            this.ProductGridView.DataSource = ProductPresent.GetPaged(pageIndex, pager.PageSize, GetLambaCondition());
            this.ProductGridView.DataBind();
            var count = 0;
            count = ProductPresent.GetList(GetLambaCondition()).Count;
            pager.IntialProperties(count);
        }

        private Expression<Func<Model.Product, bool>> GetLambaCondition()
        {
            var chname = this.txtChName.Text;
            var enname = this.txtEnName.Text;
            int inactive = (int)ProductStatus.Inactive;
            int active = (int)ProductStatus.Active;
            Expression<Func<Model.Product, bool>> searchExp = r => true;
            if (chname != null && chname != string.Empty)
                searchExp = searchExp.AndPredicate<Model.Product>(p => p.Name_CN.Contains(chname.Trim()));
            if (enname != null && enname != string.Empty)
                searchExp = searchExp.AndPredicate<Model.Product>(p => p.Name_EN.Contains(enname.Trim()));
            if (rbtnInactive.Checked == true)
                searchExp = searchExp.AndPredicate<Model.Product>(p => p.Status == inactive);
            if (rbtnActive.Checked == true)
                searchExp = searchExp.AndPredicate<Model.Product>(p => p.Status == active);

            if (!string.IsNullOrEmpty(ucLocationLab.LocationValue))
            {
                int locationid = int.Parse(ucLocationLab.LocationValue);
                var labids = LabPresent.GetList(g => g.LocationId == locationid).Select(g => g.LabId);
                searchExp = searchExp.AndPredicate<Model.Product>(p => labids.Contains(p.LabId));
                if (!string.IsNullOrEmpty(ucLocationLab.LabValue))
                {
                    int labid = int.Parse(ucLocationLab.LabValue);
                    searchExp = searchExp.AndPredicate<Model.Product>(p => p.LabId == labid);
                }
            }
            return searchExp;
        }

        public override void InitControlResource()
        {
            lblChName.Text = ManageResource.mgr_lblProductNameCN;
            lblEnName.Text = ManageResource.mgr_lblProductNameEN;
            btnQuery.Text = ManageResource.mgr_btnQuery;
            btnAdd.Text = ManageResource.mgr_btnCommonAdd;
            lblStatus.Text = ManageResource.mgr_lblProductStatus;
            rbtnActive.Text = ManageResource.mgr_lblActive;
            rbtnInactive.Text = ManageResource.mgr_lblInactive;
            rtbnAll.Text = ManageResource.mgr_lblAll;
            int i = 0;
            ProductGridView.Columns[i++].HeaderText = NameResource.No;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblLocationName;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblLabName;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblProductNameCN;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblProductNameEN;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblProductStatus;
            ProductGridView.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void ProductGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            var ID = this.ProductGridView.DataKeys[i][ProductId].ToString();
            int productid = int.Parse(ID);
            ProductPicPresent.Delete(ProductPicPresent.GetList(g => g.ProductId == productid));
            ProductPresent.Delete(productid);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Product/Update.aspx?pagestatus=new");
        }

        protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            string ID = this.ProductGridView.DataKeys[i][ProductId].ToString();
            Response.Redirect("/Product/Update.aspx?pagestatus=edit&Id=" + ID);
        }

        protected void ProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                (e.Row.Cells[0].FindControl("lblNum") as Label).Text = (e.Row.RowIndex + 1 + (pager.CurrentPageIndex) * pager.PageSize).ToString();
                Model.Product product = e.Row.DataItem as Model.Product;
                if (product != null)
                {
                    Model.Lab lab = LabPresent.GetById(product.LabId);
                    (e.Row.Cells[1].FindControl("labNamelab") as Label).Text = lab.Name;
                    Model.Location location = LocationPresent.GetById(lab.LocationId);
                    (e.Row.Cells[0].FindControl("locNamelab") as Label).Text = location.Name_CN;
                    Label lblSatus = e.Row.Cells[4].FindControl("lblSatus") as Label;
                    if (product.Status == (int)ProductStatus.Active)
                        lblSatus.Text = NameResource.Product_Status_Active;
                    else if (product.Status == (int)ProductStatus.Inactive)
                        lblSatus.Text = NameResource.Product_Status_Inactive;
                    (e.Row.Cells[5].FindControl("BtnDelete") as LinkButton).Text = ManageResource.mgr_btnDelete;
                    (e.Row.Cells[5].FindControl("BtnDelete") as LinkButton).Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                    (e.Row.Cells[5].FindControl("BtnUpdate") as LinkButton).Text = ManageResource.mgr_btnEdit;
                }
            }

        }

    }
}